import {
  isWhatType
} from '../utils/utilsNew';

const rangeSalesPrice = [{
  label: '100万及以下',
  value: '0,100'
}, {
  label: '100-150万',
  value: '100,150'
}, {
  label: '150-200万',
  value: '150,200'
}, {
  label: '200-300万',
  value: '200,300'
}, {
  label: '300-500万',
  value: '300,500'
}, {
  label: '500-800万',
  value: '500,800'
}, {
  label: '800-1000万',
  value: '800,1000'
}, {
  label: '1000万及以上',
  value: '1000,'
}]

const rangeRentPrice = [{
  label: '1000元及以下',
  value: '0,1000'
}, {
  label: '1000-1500元',
  value: '1000,1500'
}, {
  label: '1500-2000元',
  value: '1500,2000'
}, {
  label: '2000-3000元',
  value: '2000,3000'
}, {
  label: '3000-5000元',
  value: '3000,5000'
}, {
  label: '5000-8000元',
  value: '5000,8000'
}, {
  label: '8000-12000元',
  value: '8000,12000'
}, {
  label: '12000元及以上',
  value: '12000,'
}]

const rangeAddTime = [{
  label: '今天',
  value: '1'
}, {
  label: '近3天',
  value: '3'
}, {
  label: '近7天',
  value: '7'
}, {
  label: '近30天',
  value: '30'
}]

const rangeArea = [{
  label: '50㎡及以下',
  value: '0,50'
}, {
  label: '50-70㎡',
  value: '50,70'
}, {
  label: '70-90㎡',
  value: '70,90'
}, {
  label: '90-110㎡',
  value: '90,110'
}, {
  label: '110-130㎡',
  value: '110,130'
}, {
  label: '130-150㎡',
  value: '130,150'
}, {
  label: '150㎡及以上',
  value: '150,'
}]

const rangeHouseType = [{
  label: '一室',
  value: '1'
}, {
  label: '二室',
  value: '2'
}, {
  label: '三室',
  value: '3'
}, {
  label: '四室',
  value: '4'
}, {
  label: '五室',
  value: '5'
}, {
  label: '五室及以上',
  value: '6'
}]

const rangeSalesType = [{
  label: '二手',
  value: '1'
}, {
  label: '出租',
  value: '2'
}]
//业主房源特有
const rangeDistance = [{
    label: '附近(500米)',
    value: '500'
  }, {
    label: '1000米',
    value: '1000'
  },
  {
    label: '2000米',
    value: '2000'
  },
  {
    label: '3000米',
    value: '3000'
  }
]
const rangeSalesPriceX = [{
  label: '0-100万',
  value: '0,100'
}, {
  label: '100-150万',
  value: '100,150'
}, {
  label: '150-200万',
  value: '150,200'
}, {
  label: '200-300万',
  value: '200,300'
}, {
  label: '300-500万',
  value: '300,500'
}, {
  label: '500-800万',
  value: '500,800'
}, {
  label: '800-1000万',
  value: '800,1000'
}, {
  label: '1000万及以上',
  value: '1000,'
}]

const rangeRentPriceX = [{
  label: '0-1000元',
  value: '0,1000'
}, {
  label: '1000-1500元',
  value: '1000,1500'
}, {
  label: '1500-2000元',
  value: '1500,2000'
}, {
  label: '2000-3000元',
  value: '2000,3000'
}, {
  label: '3000-5000元',
  value: '3000,5000'
}, {
  label: '5000-8000元',
  value: '5000,8000'
}, {
  label: '8000-12000元',
  value: '8000,12000'
}, {
  label: '12000元及以上',
  value: '12000,'
}]

const rangeAddTimeX = [{
    label: '今天',
    value: '1'
  }, {
    label: '近3天',
    value: '3'
  }, {
    label: '近7天',
    value: '7'
  }, {
    label: '近14天',
    value: '14'
  }, {
    label: '近30天',
    value: '30'
  }, {
    label: '近90天',
    value: '90'
  },
  {
    label: '近180天',
    value: '180'
  },
  {
    label: '不限',
    value: 'null'
  }
]

function parseFilter(data) {
  let newData = {}
  Object.keys(data).forEach(item => {
    switch (isWhatType(data[item])) {
      case 'String':
        newData[item] = data[item] ? Number(data[item]) : null
        break
      case 'Object':
        newData[item] = [{
          min: Number(data[item].min) || null,
          max: Number(data[item].max) || null
        }]
        break
      case 'Array':
        newData[item] = data[item].map(_item => {
          if (_item.indexOf(',') !== -1) {
            let value = _item.split(',')
            return {
              min: value[0] ? Number(value[0]) : null,
              max: value[1] ? Number(value[1]) : null,
            }
          } else {
            return Number(_item)
          }
        })
        break
    }
  })
  return newData
}

function formatDistrictOptions(options) {
  if (!(options && options.length)) {
    return [];
  }
  let list = options.map((item) => {
    const list = {
      value: item.areaCode,
      label: item.areaName,
    };
    if (item.hdBizcircleList) {
      list.children = item.hdBizcircleList.map((itx) => {
        return {
          value: itx.id,
          label: itx.bizcircleName,
        };
      });
    } else {
      list.children = [];
    }
    list.children.unshift({
      value: "INFINTE",
      label: "不限",
    });
    return list;
  });
  list.unshift({
    value: "INFINTE",
    label: "不限",
  });
  return [{
    value: "area",
    label: "区域",
    children: list,
  }];
}

function parseAreaFilter(data) {
  let area = ''
  try {
    area = data[0][1] && data[0][1] !== "INFINTE" ? data[0][1] : '';
  } catch (error) {
    area = ''
  }
  return area
}

function parseDistrictFilter(data) {
  let bizCircleIds = [];
  try {
    (data || []).forEach((item) => {
      if (item[2] && item[2] !== "INFINTE") {
        bizCircleIds.push(item[2]);
      }
    });
  } catch (error) {}
  return bizCircleIds
}

export {
  rangeSalesPrice,
  rangeRentPrice,
  rangeAddTime,
  rangeArea,
  rangeHouseType,
  rangeSalesType,
  parseFilter,
  rangeDistance,
  rangeSalesPriceX,
  rangeRentPriceX,
  rangeAddTimeX,
  formatDistrictOptions,
  parseAreaFilter,
  parseDistrictFilter
}